﻿using ETC.DLEDPositionTool.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETC.DLEDPositionTool.Core
{
    public class TestConfig : Bandit.UI.MVVM.NotifyObject
    {
        private static string _TestConfigFileName = "".GetBaseDirectory() + "TestConfig.xml";

        #region 实例
        private static TestConfig _Instance;
        /// <summary>
        /// 实例
        /// </summary>
        public static TestConfig Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new TestConfig();
                    _Instance.InitTestConfig();
                }
                return _Instance;
            }
            set
            {
                _Instance = value;
            }
        }
        #endregion

        private string _Project;
        /// <summary>
        /// 专案名
        /// </summary>
        public string Project
        {
            get { return _Project; }
            set
            {
                _Project = value;
                OnPropertyChanged("Project");
            }
        }

        private string _Station;
        /// <summary>
        /// 站点名
        /// </summary>
        public string Station
        {
            get { return _Station; }
            set
            {
                _Station = value;
                OnPropertyChanged("Station");
            }
        }

        private bool _IsLoopTest;
        /// <summary>
        /// 是否循环测试
        /// </summary>
        public bool IsLoopTest
        {
            get { return _IsLoopTest; }
            set
            {
                _IsLoopTest = value;
                OnPropertyChanged("IsLoopTest");
            }
        }

        private int _LoopTestInterval;
        /// <summary>
        /// 循环测试间隔(秒)
        /// </summary>
        public int LoopTestInterval
        {
            get { return _LoopTestInterval; }
            set
            {
                _LoopTestInterval = value;
                OnPropertyChanged("LoopTestInterval");
            }
        }

        #region TestCodeModels
        //private List<TestCodeModel> _TestCodeModels;

        //public List<TestCodeModel> TestCodeModels { get { return _TestCodeModels; } set { _TestCodeModels = value; OnPropertyChanged(nameof(TestCodeModels)); } }

        #endregion

        #region 测试指令

        private int _TestModelCode;

        public int TestModelCode { get { return _TestModelCode; } set { _TestModelCode = value; OnPropertyChanged(nameof(TestModelCode)); } }

        #endregion

        #region 点灯机点灯参数

        private int _SetMCValue;

        public int SetMCValue { get { return _SetMCValue; } set { _SetMCValue = value; OnPropertyChanged(nameof(SetMCValue)); } }

        private int _SetDCValue;

        public int SetDCValue { get { return _SetDCValue; } set { _SetDCValue = value; OnPropertyChanged(nameof(SetDCValue)); } }

        private int _SetBCValue;

        public int SetBCValue { get { return _SetBCValue; } set { _SetBCValue = value; OnPropertyChanged(nameof(SetBCValue)); } }

        private int _SetScaleValue;

        public int SetScaleValue { get { return _SetScaleValue; } set { _SetScaleValue = value; OnPropertyChanged(nameof(SetScaleValue)); } }

        private int _SetIledValue;

        public int SetIledValue { get { return _SetIledValue; } set { _SetIledValue = value; OnPropertyChanged(nameof(SetIledValue)); } }

        private int _SetGCG1Value = 0;

        public int SetGCG1Value { get { return _SetGCG1Value; } set { _SetGCG1Value = value; OnPropertyChanged(nameof(SetGCG1Value)); } }

        private int _SetGCG2Value = 0;

        public int SetGCG2Value { get { return _SetGCG2Value; } set { _SetGCG2Value = value; OnPropertyChanged(nameof(SetGCG2Value)); } }

        private int _SetHLMSValue = 0;

        public int SetHLMSValue { get { return _SetHLMSValue; } set { _SetHLMSValue = value; OnPropertyChanged(nameof(SetHLMSValue)); } }

        private int _SetForLightValue;

        public int SetForLightValue { get { return _SetForLightValue; } set { _SetForLightValue = value; OnPropertyChanged(nameof(SetForLightValue)); } }

        private int _SetBackLightValue;

        public int SetBackLightValue { get { return _SetBackLightValue; } set { _SetBackLightValue = value; OnPropertyChanged(nameof(SetBackLightValue)); } }

        private int _SetLocationICValue;

        public int SetLocationICValue { get { return _SetLocationICValue; } set { _SetLocationICValue = value; OnPropertyChanged(nameof(SetLocationICValue)); } }

        private int _SetLocationLedValue;

        public int SetLocationLedValue { get { return _SetLocationLedValue; } set { _SetLocationLedValue = value; OnPropertyChanged(nameof(SetLocationLedValue)); } }

        private int _SetLocationScanNumValue;

        public int SetLocationScanNumValue { get { return _SetLocationScanNumValue; } set { _SetLocationScanNumValue = value; OnPropertyChanged(nameof(SetLocationScanNumValue)); } }
        #endregion

        #region 面积
        private int _MaxAreaValue;

        public int MaxAreaValue
        {

            get { return _MaxAreaValue; }

            set
            {
                _MaxAreaValue = value;


                OnPropertyChanged(nameof(MaxAreaValue));
            }
        }
        private int _MinAreaValue;

        public int MinAreaValue
        {

            get { return _MinAreaValue; }

            set
            {
                _MinAreaValue = value;


                OnPropertyChanged(nameof(MinAreaValue));
            }
        }

        private int _ThreasholdValue;

        public int ThreasholdValue
        {
            get { return _ThreasholdValue; }
            set
            {

                _ThreasholdValue = value;
                OnPropertyChanged(nameof(ThreasholdValue));
            }
        }
        #endregion

        #region ROI
        private int _StartPointX;
        public int StartPointX
        {
            get { return _StartPointX; }
            set
            {
                _StartPointX = value;
                OnPropertyChanged(nameof(StartPointX));
            }
        }

        private int _StartPointY;
        public int StartPointY
        {
            get { return _StartPointY; }
            set
            {
                _StartPointY = value;
                OnPropertyChanged(nameof(StartPointY));
            }
        }

        private int _RectWidth;
        public int RectWidth
        {
            get { return _RectWidth; }
            set
            {
                _RectWidth = value;
                OnPropertyChanged(nameof(RectWidth));
            }
        }

        private int _RectHeight;
        public int RectHeight
        {
            get { return _RectHeight; }
            set
            {
                _RectHeight = value;
                OnPropertyChanged(nameof(RectHeight));
            }
        }

        private int _ColsCount;
        public int ColsCount
        {
            get { return _ColsCount; }
            set
            {
                _ColsCount = value;
                OnPropertyChanged(nameof(ColsCount));
            }
        }

        private int _RowsCount;
        public int RowsCount
        {
            get { return _RowsCount; }
            set
            {
                _RowsCount = value;
                OnPropertyChanged(nameof(RowsCount));
            }
        }

        private int _ColInterval;

        public int ColInterval
        {
            get { return _ColInterval; }
            set
            {
                _ColInterval = value;
                OnPropertyChanged(nameof(ColInterval));
            }
        }

        private int _RowInterval;

        public int RowInterval
        {
            get { return _RowInterval; }
            set
            {
                _RowInterval = value;
                OnPropertyChanged(nameof(RowInterval));
            }
        }

        #endregion

        #region Single
        private int _SingleStartPointX;
        public int SingleStartPointX
        {
            get { return _SingleStartPointX; }
            set
            {
                _SingleStartPointX = value;
                OnPropertyChanged(nameof(SingleStartPointX));
            }
        }

        private int _SingleStartPointY;
        public int SingleStartPointY
        {
            get { return _SingleStartPointY; }
            set
            {
                _SingleStartPointY = value;
                OnPropertyChanged(nameof(SingleStartPointY));
            }
        }

        private int _SingleRectWidth;
        public int SingleRectWidth
        {
            get { return _SingleRectWidth; }
            set
            {
                _SingleRectWidth = value;
                OnPropertyChanged(nameof(SingleRectWidth));
            }
        }

        private int _SingleRectHeight;
        public int SingleRectHeight
        {
            get { return _SingleRectHeight; }
            set
            {
                _SingleRectHeight = value;
                OnPropertyChanged(nameof(SingleRectHeight));
            }
        }

        private int _SingleColsCount;
        public int SingleColsCount
        {
            get { return _SingleColsCount; }
            set
            {
                _SingleColsCount = value;
                OnPropertyChanged(nameof(SingleColsCount));
            }
        }

        private int _SingleRowsCount;
        public int SingleRowsCount
        {
            get { return _SingleRowsCount; }
            set
            {
                _SingleRowsCount = value;
                OnPropertyChanged(nameof(SingleRowsCount));
            }
        }

        private int _SingleColInterval;

        public int SingleColInterval
        {
            get { return _SingleColInterval; }
            set
            {
                _SingleColInterval = value;
                OnPropertyChanged(nameof(SingleColInterval));
            }
        }

        private int _SingleRowInterval;

        public int SingleRowInterval
        {
            get { return _SingleRowInterval; }
            set
            {
                _SingleRowInterval = value;
                OnPropertyChanged(nameof(SingleRowInterval));
            }
        }
        #endregion

        private Bandit.BanditCollection<MyROIRect> _ROIRectList;

        public Bandit.BanditCollection<MyROIRect> ROIRectList
        {
            get
            {
                return _ROIRectList;
            }
            set
            {
                _ROIRectList = value;
                OnPropertyChanged(nameof(ROIRectList));
            }
        }


        private Bandit.BanditCollection<MyROIRect> _SingleROIRectList;

        public Bandit.BanditCollection<MyROIRect> SingleROIRectList
        {
            get
            {
                return _SingleROIRectList;
            }
            set
            {
                _SingleROIRectList = value;
                OnPropertyChanged(nameof(SingleROIRectList));
            }
        }


        private string _ROIImagePath;

        public string ROIImagePath
        {
            get
            {
                return _ROIImagePath;
            }
            set
            {
                _ROIImagePath = value;
                OnPropertyChanged(nameof(ROIImagePath));
            }
        }

        private int _ICCount;
        public int ICCount
        {
            get
            {
                return _ICCount;
            }
            set
            {
                _ICCount = value;
                OnPropertyChanged(nameof(ICCount));
            }
        }

        private int _LedCount;
        public int LedCount
        {
            get
            {
                return _LedCount;
            }
            set
            {
                _LedCount = value;
            
                OnPropertyChanged(nameof(LedCount));
            }
        }
        public TestConfig()
        {

        }

        public void InitTestConfig()
        {
            this.LoopTestInterval = 10;

            this.SetMCValue = 1;

            this.SetBCValue = 1;

            this.SetDCValue= 1;

            this.SetScaleValue = 1;

            this.SetIledValue = 1;

            this.SetForLightValue= 1000;

            this.SetBackLightValue= 10;

            this.SetLocationICValue = 1;

            this.SetLocationLedValue = 1;

            MaxAreaValue = 12000;

            MinAreaValue = 500;

            ThreasholdValue = 100;

            ICCount = 11;

            LedCount= 48;
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void SaveConfig(string path)
        {
            Bandit.Helpers.ConfigHelper.XmlFileSerialize<TestConfig>(TestConfig.Instance, path);
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        public static void SaveConfig()
        {
            SaveConfig(TestConfig._TestConfigFileName);
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void LoadConfig(string path)
        {
            if (System.IO.File.Exists(path))
            {
                TestConfig._Instance = Bandit.Helpers.ConfigHelper.XmlFileDeserialize<TestConfig>(path);

                //if (TestConfig._Instance.Product.ColumnCount == 0)
                //{
                //    TestConfig._Instance.Product.ColumnCount = 8;
                //}
                //else
                //{
                //    foreach (var item in TestConfig._Instance.Product.Pins)
                //    {
                //        item.Name = item.Limit.Name;
                //    }
                //}

                if (path != TestConfig._TestConfigFileName)
                {
                    SaveConfig();
                }
            }
            else
            {
                if (path == TestConfig._TestConfigFileName)
                {
                    TestConfig.Instance = new TestConfig();
                    TestConfig.Instance.InitTestConfig();
                }
            }
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        public static void LoadConfig()
        {
            LoadConfig(TestConfig._TestConfigFileName);
        }

        public override string ToString()
        {
            return string.Format("{0}-{1}", this.Project, this.Station);
        }
    }

    public class TestCodeModel
    {
        public string Name { get; set; }

        public int TestModelCode { get; set; }

        public TestCodeModel() { }

        public TestCodeModel(string Name, int TestModelCode)
        {
            this.Name = Name;
            this.TestModelCode = TestModelCode;

        }
    }
}
